package shen.yue.example.one.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gm.sdk.use.AccountManage;
import com.gm.sdk.use.CarMachineIml;

import shen.yue.example.one.BaseActivity;
import shen.yue.example.one.R;
import shen.yue.example.one.google.zxing.activity.CaptureActivity;
import shen.yue.example.one.google.zxing.encoding.EncodingHandler;
import shen.yue.example.one.utils.CommonUtils;

/**
 * Created by jiangzhuolei on 2017/8/2.
 */

public class Activity6 extends BaseActivity implements View.OnClickListener {



    private Button openQrCodeScan;
    private Button login;
    private EditText text;
    private Button CreateQrCode;
    private ImageView QrCode;
    private TextView qrCodeText;

    //打开扫描界面请求码
    private int REQUEST_CODE = 0x01;
    //扫描成功返回码
    private int RESULT_OK = 0xA1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity6);
        initView();
    }

    private void initView() {
        text = (EditText) findViewById(R.id.text);
        QrCode = (ImageView) findViewById(R.id.QrCode);
        qrCodeText = (TextView) findViewById(R.id.qrCodeText);
        openQrCodeScan = (Button) findViewById(R.id.openQrCodeScan);
        CreateQrCode = (Button) findViewById(R.id.CreateQrCode);
        login = (Button) findViewById(R.id.login);
        openQrCodeScan.setOnClickListener(this);
        CreateQrCode.setOnClickListener(this);
        login.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.openQrCodeScan:
                //打开二维码扫描界面
                if(CommonUtils.isCameraCanUse()){
                    Intent intent = new Intent(Activity6.this, CaptureActivity.class);
                    startActivityForResult(intent, REQUEST_CODE);
                }else{
                    Toast.makeText(this,"请打开此应用的摄像头权限！",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.CreateQrCode:
                try {
                    //获取输入的文本信息
                    String str = text.getText().toString().trim();
                    if(str != null && !"".equals(str.trim())){
                        //根据输入的文本生成对应的二维码并且显示出来
                        Bitmap mBitmap = EncodingHandler.createQRCode(text.getText().toString(), 500);
                        if(mBitmap != null){
                            Toast.makeText(this,"二维码生成成功！",Toast.LENGTH_SHORT).show();
                            QrCode.setImageBitmap(mBitmap);
                        }
                    }else{
                        Toast.makeText(this,"文本信息不能为空！",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.login:
                send();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //扫描结果回调
        if (resultCode == RESULT_OK) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("qr_scan_result");
            //将扫描出的信息显示出来
            Log.i(TAG,"scanResult:" + scanResult);
            qrCodeText.setText(scanResult);
        }
    }

    private void send(){
        String str = qrCodeText.getText().toString().trim();

        String[] strS = str.split("/");

        final String qrCodeId = strS[strS.length-1];

        new Thread(new Runnable() {
            @Override
            public void run() {
                AccountManage.getInstance(Activity6.this).login("13917275427","Pass1234");
                String ticket = CarMachineIml.getInstance(Activity6.this).getExchangeTicket("9qEirm7qsUkQm7OFVVq078u3u3ZrH87636Vb7RAGv5y7d0ih75G8mJJJYb7");
                String str = AccountManage.getInstance(Activity6.this).updateQRCodeInfo(qrCodeId,ticket);
            }
        }).start();

    }


}
